////////////////////////////////////////////////////////////////////////////////
// This file is part of the Mist compiler.                                    //
//                                                                            //
// Copyright (C) 2008, 2009 The Mist project                                  //
//                                                                            //
// For a list of contributors, see the COMMITTERS file.                       //
//                                                                            //
// The Mist compiler is free software: you can redistribute it and/or modify  //
// it under the terms of the GNU General Public License as published by       //
// the Free Software Foundation, either version 3 of the License, or          //
// (at your option) any later version.                                        //
//                                                                            //
// The Mist compiler is distributed in the hope that it will be useful,       //
// but WITHOUT ANY WARRANTY; without even the implied warranty of             //
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the              //
// GNU General Public License for more details.                               //
//                                                                            //
// You should have received a copy of the GNU General Public License          //
// in the COPYING file. If not, see <http://www.gnu.org/licenses/>.           //
////////////////////////////////////////////////////////////////////////////////

///
/// \file
/// Contains the implementation of the Messages methods.
///

#include "messages.hpp"

////////////////////////////////////////////////////////////////////////////////
// Includes                                                                   //
////////////////////////////////////////////////////////////////////////////////

#include <boost/foreach.hpp>
#define foreach BOOST_FOREACH

#include "common/assert.hpp"
#include "common/types.hpp"

////////////////////////////////////////////////////////////////////////////////
// Function Implementations                                                   //
////////////////////////////////////////////////////////////////////////////////

void Messages::add(sptr<Message> message) {
	foreach (sptr<Message> m, _messages) {
		if (m->can_merge_with(message)) {
			m->merge_with(message);
			return;
		}
	}
	
	vector<sptr<Message> >::iterator it;
	for (it = _messages.begin(); it != _messages.end(); ++it)
		if (*message < **it) break;
	_messages.insert(it, message);
}

void Messages::add(const string& code) {
	add(Message::construct(code));
}

void Messages::add(const string& code, const string& param1) {
	add(Message::construct(code, param1));
}

void Messages::add(const string& code, const string& param1, const string& param2) {
	add(Message::construct(code, param1, param2));
}

void Messages::add(const string& code, const string& param1, const string& param2, const string& param3) {
	add(Message::construct(code, param1, param2, param3));
}

void Messages::add(const string& code, const Location& location) {
	add(Message::construct(code, location));
}

void Messages::add(const string& code, const Location& location, const string& param1) {
	add(Message::construct(code, location, param1));
}

void Messages::add(const string& code, const Location& location, const string& param1, const string& param2) {
	add(Message::construct(code, location, param1, param2));
}

void Messages::add(const string& code, const Location& location, const string& param1, const string& param2, const string& param3) {
	add(Message::construct(code, location, param1, param2, param3));
}

uint Messages::count() const {
	return _messages.size();
}

uint Messages::count(MessageSeverity severity) const {
	uint result = 0;
	foreach (sptr<Message> m, _messages)
		result += (the(MessageInfo).severity(m->code()) == severity);
	
	return result;
}

uint Messages::count(const string& code) const {
	uint result = 0;
	foreach (sptr<Message> m, _messages)
		result += (m->code() == code);
	
	return result;
}

ostream& Messages::send_to_stream(ostream& out) const {
	foreach (sptr<Message> m, _messages)
		out << *m;
	
	return out;
}
